package com.itheima.health.config;

import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: liangWeiQUQ
 * @Description: 初始化用户角色信息-模拟数据库
 * @DateTime: 2021/3/6 17:46
 **/
@Component
public class CustomerUserDetail implements UserDetailsService {
    //创建模拟数据库的容器
    private static Map<String, User> userDd = new HashMap<>();
    /**
       * @Author: 传闻中的梁大侠
       * @Date: 17:48 2021/3/6
       * @Parms
       * @ReturnType:
       * @Description: 初始话用户角色信息-模拟数据库
     */

    static {
        //1.初始化权限
        Permission addPerms = new Permission("添加权限", "add");
        Permission findPerms = new Permission("查询权限", "find");
        Permission updatePerms = new Permission("修改权限", "update");
        Permission delPerms = new Permission("删除权限", "del");
        //2.初始化角色信息
        Role  adminRole = new Role("超级管理员", "ROLE_ADMIN");
        adminRole.getPermissions().add(addPerms);
        adminRole.getPermissions().add(findPerms);
        adminRole.getPermissions().add(updatePerms);
        adminRole.getPermissions().add(delPerms);


        Role role1 = new Role("角色1", "ROLE_01");
        role1.getPermissions().add(addPerms);//给角色1赋值添加的权限
        role1.getPermissions().add(findPerms);
        //用户密码加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        bCryptPasswordEncoder.encode("1234");

        Role role2 = new Role("角色2", "ROLE_02");
        role2.getPermissions().add(findPerms);//给角色二赋值查询的权限
        //3.初始化用户信息
        User adminUser = new User();
        adminUser.setUsername("admin");
        adminUser.setPassword("{bcrypt}$2a$10$gGZICp1r0zS7.3hx/PYSeuUs7yuEXunsu/XNbwuBaJ.mH7vzickrO");
        adminUser.getRoles().add(adminRole);
        //添加到虚拟的数据库中
        userDd.put(adminUser.getUsername(), adminUser);
        //------------------------------------------------------
        User sanUser = new User();
        sanUser.setUsername("zhangsan");
        sanUser.setPassword("{bcrypt}$2a$10$gGZICp1r0zS7.3hx/PYSeuUs7yuEXunsu/XNbwuBaJ.mH7vzickrO");
        sanUser.getRoles().add(role1);
        //添加到虚拟的数据库中
        userDd.put(sanUser.getUsername(), sanUser);
        //-------------------------------------------------------
        User lisiUser = new User();
        lisiUser.setUsername("lisi");
        lisiUser.setPassword("{noop}1234");
        lisiUser.getRoles().add(role2);
        //添加到虚拟的数据库中
        userDd.put(lisiUser.getUsername(), lisiUser);
    }

    /**
     * @Author: 传闻中的梁大侠
     * @Date: 18:00 2021/3/6
     * @Parms [s]
     * @ReturnType: org.springframework.security.core.userdetails.UserDetails
     * @Description: 加载用户权限信息
     */
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //String s根据用户名判断用户所对应的权限
        User user = userDd.get(s);
        if (user == null) {
            return null;
        }
        //1.获取角色
        StringBuilder authStr = new StringBuilder();
        Set<Role> roles = user.getRoles();
        for (Role role : roles) {
            authStr.append(role.getKeyword());//往容器里边添加角色
            authStr.append(",");
            //2.获取权限
            Set<Permission> permissions = role.getPermissions();
            for (Permission permission : permissions) {
                authStr.append(permission.getKeyword());//往容器里边添加角色对应的权限
                authStr.append(",");
            }
        }
        if (authStr.length() > 0) {
            //删除容器中的最末尾的‘  ，’
            authStr.deleteCharAt(authStr.length() - 1);
        }
        return new org.springframework.security.core.userdetails.User(s, user.getPassword()
                , AuthorityUtils.commaSeparatedStringToAuthorityList(authStr.toString()));
    }
}

